home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / program / tcpdumpb.zip / libpcap / scanner.l < prev    next >
Text File  |  1996-07-17  |  4KB  |  202 lines

  1. %{
  2. /*
  3.  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
  4.  *    The Regents of the University of California.  All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that: (1) source code distributions
  8.  * retain the above copyright notice and this paragraph in its entirety, (2)
  9.  * distributions including binary code include the above copyright notice and
  10.  * this paragraph in its entirety in the documentation or other materials
  11.  * provided with the distribution, and (3) all advertising materials mentioning
  12.  * features or use of this software display the following acknowledgement:
  13.  * ``This product includes software developed by the University of California,
  14.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  15.  * the University nor the names of its contributors may be used to endorse
  16.  * or promote products derived from this software without specific prior
  17.  * written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  */
  22.  
  23. #ifndef lint
  24. static char rcsid[] =
  25.     "@(#) $Header: scanner.l,v 1.53 96/07/17 00:11:34 leres Exp $ (LBL)";
  26. #endif
  27.  
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30.  
  31. #include <ctype.h>
  32. #include <unistd.h>
  33.  
  34. #include "pcap-int.h"
  35.  
  36. #include "gencode.h"
  37. #include <pcap-namedb.h>
  38. #include "tokdefs.h"
  39.  
  40. #include "gnuc.h"
  41. #ifdef HAVE_OS_PROTO_H
  42. #include "os-proto.h"
  43. #endif
  44.  
  45. static int stoi(char *);
  46. static inline int xdtoi(int);
  47.  
  48. #ifdef FLEX_SCANNER
  49. #undef YY_INPUT
  50. #define YY_INPUT(buf, result, max)\
  51.  {\
  52.     char *src = in_buffer;\
  53.     int i;\
  54. \
  55.     if (*src == 0)\
  56.         result = YY_NULL;\
  57.     else {\
  58.         for (i = 0; *src && i < max; ++i)\
  59.             buf[i] = *src++;\
  60.         in_buffer += i;\
  61.         result = i;\
  62.     }\
  63.  }
  64. #else
  65. #undef getc
  66. #define getc(fp)  (*in_buffer == 0 ? EOF : *in_buffer++)
  67. #endif
  68.  
  69. #define yylval pcap_lval
  70. extern YYSTYPE yylval;
  71.  
  72. static char *in_buffer;
  73.  
  74. %}
  75.  
  76. N        ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
  77. B        ([0-9A-Fa-f][0-9A-Fa-f]?)
  78.  
  79. %a 3000
  80.  
  81. %%
  82. dst        return DST;
  83. src        return SRC;
  84.  
  85. link|ether|ppp|slip  return LINK;
  86. fddi        return LINK;
  87. arp        return ARP;
  88. rarp        return RARP;
  89. ip        return IP;
  90. tcp        return TCP;
  91. udp        return UDP;
  92. icmp        return ICMP;
  93. igmp        return IGMP;
  94. igrp        return IGRP;
  95.  
  96. atalk        return ATALK;
  97. decnet        return DECNET;
  98. lat        return LAT;
  99. sca        return SCA;
  100. moprc        return MOPRC;
  101. mopdl        return MOPDL;
  102.  
  103. host        return HOST;
  104. net        return NET;
  105. mask        return MASK;
  106. port        return PORT;
  107. proto        return PROTO;
  108.  
  109. gateway        return GATEWAY;
  110.  
  111. less        return LESS;
  112. greater        return GREATER;
  113. byte        return BYTE;
  114. broadcast    return TK_BROADCAST;
  115. multicast    return TK_MULTICAST;
  116.  
  117. and|"&&"    return AND;
  118. or|"||"        return OR;
  119. not        return '!';
  120.  
  121. len|length    return LEN;
  122. inbound        return INBOUND;
  123. outbound    return OUTBOUND;
  124.  
  125. [ \n\t]            ;
  126. [+\-*/:\[\]!<>()&|=]    return yytext[0];
  127. ">="            return GEQ;
  128. "<="            return LEQ;
  129. "!="            return NEQ;
  130. "=="            return '=';
  131. "<<"            return LSH;
  132. ">>"            return RSH;
  133. {N}            { yylval.i = stoi((char *)yytext); return NUM; }
  134. ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N})    {
  135.             yylval.s = sdup((char *)yytext); return HID;
  136. }
  137. {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
  138.               return EID; }
  139. {B}:+({B}:+)+        { bpf_error("bogus ethernet address %s", yytext); }
  140. [A-Za-z][-_.A-Za-z0-9]*    { yylval.s = sdup((char *)yytext); return ID; }
  141. "\\"[^ !()\n\t]+    { yylval.s = sdup((char *)yytext + 1); return ID; }
  142. [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+    { bpf_error("illegal token: %s\n", yytext); }
  143. .            { bpf_error("illegal char '%c'", *yytext); }
  144. %%
  145. void
  146. lex_init(buf)
  147.     char *buf;
  148. {
  149.     in_buffer = buf;
  150. }
  151.  
  152. /*
  153.  * Also define a yywrap.  Note that if we're using flex, it will
  154.  * define a macro to map this identifier to pcap_wrap.
  155.  */
  156. int
  157. yywrap()
  158. {
  159.     return 1;
  160. }
  161.  
  162. /* Hex digit to integer. */
  163. static inline int
  164. xdtoi(c)
  165.     register int c;
  166. {
  167.     if (isdigit(c))
  168.         return c - '0';
  169.     else if (islower(c))
  170.         return c - 'a' + 10;
  171.     else
  172.         return c - 'A' + 10;
  173. }
  174.  
  175. /*
  176.  * Convert string to integer.  Just like atoi(), but checks for
  177.  * preceding 0x or 0 and uses hex or octal instead of decimal.
  178.  */
  179. static int
  180. stoi(s)
  181.     char *s;
  182. {
  183.     int base = 10;
  184.     int n = 0;
  185.  
  186.     if (*s == '0') {
  187.         if (s[1] == 'x' || s[1] == 'X') {
  188.             s += 2;
  189.             base = 16;
  190.         }
  191.         else {
  192.             base = 8;
  193.             s += 1;
  194.         }
  195.     }
  196.     while (*s)
  197.         n = n * base + xdtoi(*s++);
  198.  
  199.     return n;
  200. }
  201.  
  202.